<?php

namespace App\Http\Controllers\Backend\Product;

 
use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use App\Repository\Repositories\Interfaces\ProductsRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Exception;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;
 
 
class ProductController extends BaseController
{
    
    protected $request;
    protected $product_repo;
    protected $product_sku_repo;
    
    public function __construct(ProductsRepository $product_repo,ProductSkusRepository $product_sku_repo,Request $request)
    {
        $this->request = $request;
        $this->product_repo = $product_repo;
        $this->product_sku_repo = $product_sku_repo;
    }
    
    public function index(Request $request)
    {
        $products = $this->product_repo->paginate($this->perPage);
        
        return $this->success(ErrorConst::SUCCESS_CODE, $products,true);
    }
    
    public function show($id)
    {
        $product = $this->product_repo->with('skus')->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE, $product);
    }
    
    public function create(Request $request)
    {
 
        $this->validate($request,[
            'name'=> 'required|string',
            'brand_id'=>'required|exists:product_brands,id',
            'sort'=>'integer',
        ]);
        
        $data = $request->all();
        $data['status'] = 0;
        $product = $this->product_repo->createNew($data);
      
        if($product){
            return $this->success(ErrorConst::SUCCESS_CODE, $product['data']);
        }else{
            abort(422);
        }
        
    }
    
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'=> 'string',
            'initial' => 'string|size:1',
            'sort'=>'integer',
        ]);
        $data = $request->all();
      
        $skus = isset($data['skus']) && $data['skus'] ? $data['skus'] : "";
        if($skus){
            unset($data['skus']);
        }
        
        DB::beginTransaction();
        try {
            $ret = $this->product_repo->update($data, $id);
            
            if($skus){
                $skus= json_decode($skus,true);
                foreach($skus as &$sku){
                    
                    if(!isset($sku['price']) || !$sku['price']){
                        $sku['price'] = 0;
                    }
                    
                    $ret = $this->product_sku_repo->update($sku, $sku['id']);
                }
                
            }
            DB::commit();
        } catch (Exception $e) {
            DB::rollback(); //事务回滚;
            return $this->error(ErrorConst::FAILED_CODE_MSG);
        }
        
        return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
    }
    
    public function delete($id)
    {
        $ret = $this->product_repo->delete($id);
        
        
        if ($ret) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }
    /**
     * @TODO 设置商品是否 推荐至首页
     * */
    public function recommend($id){
        $status = $this->request->get('is_index_recommend');
        $sort = $this->request->get('index_sort');

        $ret = $this->product_repo->setRecommend($id,$status,$sort);

        if ($ret['flag']) {
            return $this->success('修改成功');
        } else {
            return $this->error($ret['info']);
        }
    }

    public function batchStatus(Request $request){
        $this->validate($request,[
            'ids'=> 'required|array',
            'status'=>'required|integer',
        ]);
        $data = $request->all(['ids','status']);
        
        $ret = $this->product_repo->batchStatus($data);
        if ($ret) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
    }
    
   
}
